package controller;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.BookingDTO;
import model.UserDTO;
import bizlogic.BookingManager;

/**
 * Servlet implementation class ProcessBookingServlet
 */
@WebServlet("/ProcessBookingServlet")
public class ProcessBookingServlet extends HttpServlet
{
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ProcessBookingServlet()
	{
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		// TODO Auto-generated method stub
		try
		{
			processRequest(request, response);
		} catch (ParseException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		// TODO Auto-generated method stub
		try
		{
			processRequest(request, response);
		} catch (ParseException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException,
			ParseException
	{
		// TODO Auto-generated method stub
		// setting regular parameters
		BookingManager bm = new BookingManager();
		BookingDTO booking = new BookingDTO();
		booking.setPriority(Integer.parseInt(request.getParameter("priority")));
		booking.setContactDetail(request.getParameter("contactDetail"));
		booking.setReason(request.getParameter("reason"));
		booking.setEmergency(Boolean.valueOf(request.getParameter("emergency")));
		booking.setFacilityName(request.getParameter("facilityName"));
		booking.setId(request.getParameter("bookingId"));
;

		String ins = (String) request.getParameter("ins");

		// setting time related parameters
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		String start = request.getParameter("startDate");
		String end = request.getParameter("endDate");

		java.util.Date st = null;
		java.util.Date ed = null;
		try
		{
			System.out.println(start);
			st = df.parse(start);
			System.out.println(st);
			System.out.println(end);
			ed = df.parse(end);
			System.out.println(ed);
		} catch (ParseException e1)
		{
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		java.sql.Date ds = new java.sql.Date(st.getTime());
		java.sql.Date dsend = new java.sql.Date(ed.getTime());
		java.sql.Date bookOn =new java.sql.Date(df.parse((df.format(new Date()))).getTime());
		System.out.println("the date book"+bookOn);// Mon Jul 05 00:00:00 IST 2010

		booking.setStartDate(ds);
		booking.setEndDate(dsend);
		ArrayList<BookingDTO> test=bm.getAllBooking();
		int num = test.size();
		num = Integer.parseInt(test.get(num-1).getId()) + 1;

		if (ins.matches("true"))
		{
			// set insert-related parameter
			UserDTO ud = (UserDTO) request.getSession().getAttribute("user");
			booking.setBookOn(bookOn);
			booking.setUserId(ud.getUsername());
			booking.setId(Integer.toString(num));
			booking.setStatus("Pending");
			
			System.out.println("Before Insert");
			bm.insertBooking(booking);
			System.out.println("After Insert");
			/*
			 * HttpSession hs = request.getSession(false); BookingDTO bookd =
			 * (BookingDTO)hs.getAttribute("user");
			 * boong.setUserId(bookd.getUserId()); RequestDispatcher rd =
			 * request.getRequestDispatcher("MakeBooking.jsp");
			 * rd.forward(request, response);
			 */
		} else
		{
			booking.setStatus(request.getParameter("status"));
			System.out.println("Before Update");
			bm.updateBooking(booking);
			System.out.println("After Update");

			/*
			 * HttpSession hs = request.getSession(false); bd =
			 * (BookingDTO)hs.getAttribute("user");
			 * booking.setUserId(bd.getUserId()); RequestDispatcher rd =
			 * request.getRequestDispatcher("MakeBooking.jsp");
			 * rd.forward(request, response);
			 */
		}
//		ArrayList<BookingDTO> data = bm.getAllBooking();
//		request.setAttribute("booking", data);
//		RequestDispatcher rd = request.getRequestDispatcher("/LoadBooking");
//		try
//		{
//			rd.forward(request, response);
//		} catch (ServletException e)
//		{
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (IOException e)
//		{
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		response.sendRedirect("LoadBooking");
	}

}
